package com.gw.detail.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gw.detail.domain.*;
import com.gw.detail.entity.dto.ParticipantDTO;
import com.gw.detail.service.*;
import com.ruoyi.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author WangBin
 * @version 1.0
 * @date 2021/5/5 15:52
 */
@Service
public class QueryServiceImpl implements QueryService {
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private IBeteilService beteilService;
    @Autowired
    private IAnprallService anprallService;
    @Autowired
    private IPersonDataService personDataService;
    @Autowired
    private IVerlService verlService;
    @Autowired
    private IVerluebService verluebService;
    @Autowired
    private IRettungService rettungService;
    @Autowired
    private IFzgService fzgService;
    @Autowired
    private ICrashperService crashperService;
    @Autowired
    private IZweiradService zweiradService;
    @Autowired
    private ISitzeService sitzeService;
    @Autowired
    private IKbadatenService kbadatenService;
    @Autowired
    private IPkwService pkwService;
    @Autowired
    private IHaengerService haengerService;
    @Autowired
    private ILkwService lkwService;
    @Autowired
    private IPkwbService pkwbService;
    @Autowired
    private ILkwbService lkwbService;
    @Autowired
    private IFzgaService fzgaService;
    @Autowired
    private IInnenService iInnenService;
    @Autowired
    private IFzgiService fzgiService;
    @Autowired
    private IFzgasService fzgasService;
    @Autowired
    private IUmweltService umweltService;
    @Autowired
    private IVuanzService vuanzService;
    @Autowired
    private IStrasseService strasseService;
    @Autowired
    private IBefragService befragService;
    @Autowired
    private IVuanzbetService vuanzbetService;
    @Autowired
    private IVuanzmfService vuanzmfService;
    @Autowired
    private IFgdatService fgdatService;
    @Autowired
    private IMpersService mpersService;
    @Autowired
    private IZwdatService zwdatService;
    @Autowired
    private IRekoallgService rekoallgService;
    @Autowired
    private IRekoService rekoService;

    @Override
    public ParticipantDTO getByFall(Long fall) {
        ParticipantDTO participantDTO = new ParticipantDTO();
        //参与方数据
        CompletableFuture<List<Beteil>> f28 = CompletableFuture.supplyAsync(() ->
                        beteilService.list(Wrappers.lambdaQuery(Beteil.class).eq(Beteil::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setBeteil(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //人员数据
        CompletableFuture<List<PersonData>> f3 = CompletableFuture.supplyAsync(() ->
                        personDataService.list(Wrappers.lambdaQuery(PersonData.class).eq(PersonData::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setPersonData(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //单个损伤
        CompletableFuture<List<Verl>> f5 = CompletableFuture.supplyAsync(() ->
                        verlService.list(Wrappers.lambdaQuery(Verl.class).eq(Verl::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setVerl(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //伤情概述
        CompletableFuture<List<Verlueb>> f4 = CompletableFuture.supplyAsync(() ->
                        verluebService.list(Wrappers.lambdaQuery(Verlueb.class).eq(Verlueb::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setVerlueb(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //救援
        CompletableFuture<List<Rettung>> f6 = CompletableFuture.supplyAsync(() ->
                        rettungService.list(Wrappers.lambdaQuery(Rettung.class).eq(Rettung::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setRettung(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //车辆数据
        CompletableFuture<List<Fzg>> f2 = CompletableFuture.supplyAsync(() ->
                        fzgService.list(Wrappers.lambdaQuery(Fzg.class).eq(Fzg::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setFzg(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //乘员运动
        CompletableFuture<List<Crashper>> f7 = CompletableFuture.supplyAsync(() ->
                        crashperService.list(Wrappers.lambdaQuery(Crashper.class).eq(Crashper::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setCrashper(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //两轮车技术数据
        CompletableFuture<List<Zweirad>> f12 = CompletableFuture.supplyAsync(() ->
                        zweiradService.list(Wrappers.lambdaQuery(Zweirad.class).eq(Zweirad::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setZweirad(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //约束系统和座椅
        CompletableFuture<List<Sitze>> f22 = CompletableFuture.supplyAsync(() ->
                        sitzeService.list(Wrappers.lambdaQuery(Sitze.class).eq(Sitze::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setSitze(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //车辆注册参数数据
        CompletableFuture<List<Kbadaten>> f26 = CompletableFuture.supplyAsync(() ->
                        kbadatenService.list(Wrappers.lambdaQuery(Kbadaten.class).eq(Kbadaten::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setKbadaten(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //乘用车技术数据
        CompletableFuture<List<Pkw>> f8 = CompletableFuture.supplyAsync(() ->
                        pkwService.list(Wrappers.lambdaQuery(Pkw.class).eq(Pkw::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setPkw(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //挂车技术数据
        CompletableFuture<List<Haenger>> f14 = CompletableFuture.supplyAsync(() ->
                        haengerService.list(Wrappers.lambdaQuery(Haenger.class).eq(Haenger::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setHaenger(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //货客车技术数据
        CompletableFuture<List<Lkw>> f10 = CompletableFuture.supplyAsync(() ->
                        lkwService.list(Wrappers.lambdaQuery(Lkw.class).eq(Lkw::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setLkw(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //乘用车外部损坏
        CompletableFuture<List<Pkwb>> f9 = CompletableFuture.supplyAsync(() ->
                        pkwbService.list(Wrappers.lambdaQuery(Pkwb.class).eq(Pkwb::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setPkwb(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //货客车外部损坏
        CompletableFuture<List<Lkwb>> f11 = CompletableFuture.supplyAsync(() ->
                        lkwbService.list(Wrappers.lambdaQuery(Lkwb.class).eq(Lkwb::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setLkwb(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //车辆外部
        CompletableFuture<List<Fzga>> f15 = CompletableFuture.supplyAsync(() ->
                        fzgaService.list(Wrappers.lambdaQuery(Fzga.class).eq(Fzga::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setFzga(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //车辆内部零部件损坏
        CompletableFuture<List<Innen>> f13 = CompletableFuture.supplyAsync(() ->
                        iInnenService.list(Wrappers.lambdaQuery(Innen.class).eq(Innen::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setInnen(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //车辆内部
        CompletableFuture<List<Fzgi>> f16 = CompletableFuture.supplyAsync(() ->
                        fzgiService.list(Wrappers.lambdaQuery(Fzgi.class).eq(Fzgi::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setFzgi(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //主要安全措施
        CompletableFuture<List<Fzgas>> f27 = CompletableFuture.supplyAsync(() ->
                        fzgasService.list(Wrappers.lambdaQuery(Fzgas.class).eq(Fzgas::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setFzgas(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //环境和一般事故数据
        CompletableFuture<List<Umwelt>> f1 = CompletableFuture.supplyAsync(() ->
                        umweltService.list(Wrappers.lambdaQuery(Umwelt.class).eq(Umwelt::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setUmwelt(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //交警报告中的基本数据
        CompletableFuture<List<Vuanz>> f23 = CompletableFuture.supplyAsync(() ->
                        vuanzService.list(Wrappers.lambdaQuery(Vuanz.class).eq(Vuanz::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setVuanz(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //道路数据
        CompletableFuture<List<Strasse>> f29 = CompletableFuture.supplyAsync(() ->
                        strasseService.list(Wrappers.lambdaQuery(Strasse.class).eq(Strasse::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setStrasse(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //回访数据
        CompletableFuture<List<Befrag>> f17 = CompletableFuture.supplyAsync(() ->
                        befragService.list(Wrappers.lambdaQuery(Befrag.class).eq(Befrag::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setBefrag(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //交警报告中参与方的数据
        CompletableFuture<List<Vuanzbet>> f25 = CompletableFuture.supplyAsync(() ->
                        vuanzbetService.list(Wrappers.lambdaQuery(Vuanzbet.class).eq(Vuanzbet::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setVuanzbet(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //交警报告中乘客的数据
        CompletableFuture<List<Vuanzmf>> f24 = CompletableFuture.supplyAsync(() ->
                        vuanzmfService.list(Wrappers.lambdaQuery(Vuanzmf.class).eq(Vuanzmf::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setVuanzmf(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //行人数据
        CompletableFuture<List<Fgdat>> f18 = CompletableFuture.supplyAsync(() ->
                        fgdatService.list(Wrappers.lambdaQuery(Fgdat.class).eq(Fgdat::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setFgdat(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //碰撞人员的数据
        CompletableFuture<List<Mpers>> f21 = CompletableFuture.supplyAsync(() ->
                        mpersService.list(Wrappers.lambdaQuery(Mpers.class).eq(Mpers::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setMpers(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //人员的单个碰撞
        CompletableFuture<List<Anprall>> f31 = CompletableFuture.supplyAsync(() ->
                        anprallService.list(Wrappers.lambdaQuery(Anprall.class).eq(Anprall::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setAnprall(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //两轮车使用者数据
        CompletableFuture<List<Zwdat>> f19 = CompletableFuture.supplyAsync(() ->
                        zwdatService.list(Wrappers.lambdaQuery(Zwdat.class).eq(Zwdat::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setZwdat(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //单个事件的重建数据
        CompletableFuture<List<Reko>> f20 = CompletableFuture.supplyAsync(() ->
                        rekoService.list(Wrappers.lambdaQuery(Reko.class).eq(Reko::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setReko(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        //一般重建数据
        CompletableFuture<List<Rekoallg>> f30 = CompletableFuture.supplyAsync(() ->
                        rekoallgService.list(Wrappers.lambdaQuery(Rekoallg.class).eq(Rekoallg::getFall, fall)),
                threadPoolTaskExecutor).whenComplete((r, e) -> {
            participantDTO.setRekoallg(r);
            if (ObjectUtil.isNotNull(e)) {
                throw new CustomException(e.getMessage());
            }
        });
        CompletableFuture.allOf(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13,
                f14, f15, f16, f17, f18, f19, f20, f21, f22, f23, f24, f25, f26, f27, f28, f29, f30, f31).join();
        return participantDTO;
    }
}
